'use strict';

angular.module('SystemManage').controller('AppRoleController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', 'PublicEnum', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, PublicEnum, $location, Authentication) {
        //$scope.authentication = Authentication;
       
        $scope.btnSave = function(){
            if($scope.Module.AppRole.FocusedRecord.Code == undefined || 
                $scope.Module.AppRole.FocusedRecord.Code.trim() == ""){
                PublicFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.AppRole.FocusedRecord.Name == undefined || 
                $scope.Module.AppRole.FocusedRecord.Name.trim() == ""){
                PublicFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            var apps = [];
            for(var i=0; i<$scope.Module._App.Records.length; i++){
                if($scope.Module._App.Records[i].App == undefined || 
                    $scope.Module._App.Records[i].App == null){
                    PublicFunction.alert("第" + i + "行：App应用不能为空！", "保存错误");
                    return;
                }
                for(var j=0; j<apps.length; j++){
                    if(apps[j] == $scope.Module._App.Records[i].App._id){
                        PublicFunction.alert($scope.Module._App.Records[i].App.Name + "：有重复！", "保存错误");
                        return;
                    }
                }
                apps.push($scope.Module._App.Records[i].App);
            }

            $scope.Module.AppRole.FocusedRecord.Apps = apps;
            if($scope.Module.AppRole.FocusedRecord.RowStatus == PublicEnum.RowStatus.UnChange){
                $scope.Module.AppRole.FocusedRecord.RowStatus = PublicEnum.RowStatus.Modify;
            }
            $scope.Module.AppRole.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.AppRole.cancel(displayApps);            
        };
        $scope.btnAdd = function(){
            $scope.Module.AppRole.add();
            $scope.Module._App.Records = [];
            $scope.Module._App.emitOnLoadEvent();
        };
        $scope.btnDelete = function(){
            $scope.Module.AppRole.delete(displayApps);
        };
        $scope.btnList = function(){
            $location.url('/index/AppRole/List');
        };
        $scope.btnFindChange = function(){
            displayApps();
        };
        $scope.pageChange = function(){
            displayApps();
        };
        $scope.refBeforeShow = function(refScope){
            var apps = [];
            for(var i=0; i<$scope.Module._App.Records.length; i++){
                if($scope.Module._App.Records[i].App != undefined && $scope.Module._App.Records[i].App != null){
                    apps.push($scope.Module._App.Records[i].App._id);
                }
            }
            refScope.Module.Condition = {
                IsEffective: true,
                IsShared: {$ne: true},
                _id: {$nin: apps}
            };
        }

        var displayApps = function(){
            $scope.Module._App.Records = [];
            if($scope.Module.AppRole.FocusedRecord != null && $scope.Module.AppRole.FocusedRecord.Apps != undefined && $scope.Module.AppRole.FocusedRecord.Apps != null){
                for(var i=0; i<$scope.Module.AppRole.FocusedRecord.Apps.length; i++){
                    var newRecord = $scope.Module._App.add();
                    newRecord.App = $scope.Module.AppRole.FocusedRecord.Apps[i];
                }
            }
            $scope.Module._App.emitOnLoadEvent();
        };

        //初始化
        PublicModule.initModule($scope, 'AppRole');
        $scope.Module.AppRole.Populate = {path: 'Apps'};
        $scope.Module.AppRole.AfterOnAdd = function(record){
            record.IsEffective = true;
        };

        PublicModule.initModule($scope, '_App');
        $scope.Module._App.PageSize = 10;

        if($stateParams.id) {
            $scope.Module.AppRole.findById($stateParams.id, function(record){
                $scope.Module.AppRole.FocusedRecord = record;
                displayApps();
            });
        }
        else{
            $scope.btnAdd();
        };

        //下面代码是实现批量添加角色功能
        $scope.onBeforeShow = function(){
            $scope.Module.User.Condition = { AppRoles: {$ne: $scope.Module.AppRole.FocusedRecord._id} };  //不包含当前角色
            $scope.Module.User.load();
        };

        $scope.btnBatchOK = function(){
            var userIds  = new Array();
            var selectUsers = $scope.Module.User.SelectedRecords;
            if(selectUsers.length > 0){
                for(var i=0; i<selectUsers.length; i++) {
                    userIds.push(selectUsers[i]._id);
                }

                $scope.Module.User.updateByValue({_id: {$in: userIds}}, {$push: {"AppRoles": $scope.Module.AppRole.FocusedRecord._id}}, function(data){
                    PublicFunction.tip("批量分配角色完成");
                });
            }
            else{
                PublicFunction.alert("请选择用户后再分配");
                return false;
            }
        };

        PublicModule.initModule($scope, 'User');
        $scope.Module.User.Populate = {path: 'Department', select: 'Name'};
        $scope.Module.User.PageSize = 13;
    }
]);
